<?php
class userController extends appController {    

    public function detail($id){
        if(empty($id)){
            return $this->unknownAction();
        }
        
        $data = $this->_model->read($id);
        $this->view->set('items',$data[0]);
        return $this->view();       
    }
    
    // public function create($username=null, $password=null, $email=null, $role=null, $is_active=1, $confirmed=1){
    //     if($_SERVER["REQUEST_METHOD"]=='POST'){     
    //         $id = $this->_model->create($username,$password,$email,$role,$is_active,$confirmed);
    //         if($id !== false){
    //             header('Location: blog/detail/'.$id);
    //             exit;
    //         }
    //     }
    //     return $this->view();
    // }
     //create fuction for check username and password 
    public function login($username = null, $password = null) {
        
        if($_SERVER["REQUEST_METHOD"]=='POST'){ 



                 $data = $this->_model->validation();
                

                 if($data){

                    $this->view->set('data',$data);
                    return $this->view($data);
                 }
            
            $password =$this->hashStr($password);
            $id = $this->_model->login($username,$password);
           
            if($id != null){
                $this->session->write("userId",$id);
                $this->session->write("userName",$username);
                $this->session->selflash("$username is succesfully Login");
                header('Location: dashboard/');
                exit;
            
            }                    
        }

        return $this->view();
    }
public function logout() {
                $this->session->destroy('userId');
                if($id != null){  
                    $this->session->destroy('userId');
                    header('Location: login.php');
                    exit;
                }
            return $this->view();
    }
public function signup($username=null, $password=null, $email=null){

        if($_SERVER["REQUEST_METHOD"]=='POST'){ 
            $data = $this->_model->validation();
            

            if($data){

                    $this->view->set('data',$data);
                    return $this->view();
            }
                $password =$this->hashStr($password);
                $id = $this->_model->signup($username,$password,$email);
                if($id != null){
                    header('Location: dashboard/');
                    exit;
                }
        }

        return $this->view();
    }
public function createuserprofile($Fname=null, $Lname=null, $Mname=null, $Phone=null, $Country=null, $State=null, $City=null, $Discrp=null, $Role=null, $Active=null, $Confirm=null) {
    if($_SERVER["REQUEST_METHOD"]=='POST'){     
            $id = $this->_model->createuserprofile($Fname, $Lname, $Mname, $Phone, $Country, $State, $City, $Discrp, $Role, $Active, $Confirm);
            
            if($id != null){
                header('Location: user/dashboard/');
                exit;
            }                
        }
        return $this->view();
    }
    // the function are used for insert data in userprofile tabel
    public function insertUserPrfdb($Fname, $Lname, $Mname, $Phone, $Country, $State, $City, $Discrp, $Role, $Active, $Confirm) {
        $sql = "SELECT *  FROM users WHERE id='" . $_SESSION['user_login_id'] . "' and username='" . $_SESSION['user_login_name'] . "'";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        if ($row['role'] == "") {
            $sql_insert = "INSERT INTO userprofile (user_id, first_name, last_name, middel_name, phone, country, state, city, descriptions ) "
                    . "VALUES ('" . $_SESSION['user_login_id'] . "', '$Fname', '$Lname', '$Mname', '$Phone', '$Country', '$State', '$City', '$Discrp')";
            mysql_query($sql_insert) or die(mysql_errno());
            $sql_update = "update users set role='" . $Role . "',is_active='" . $Active . "',confirmed='" . $Confirm . "' where id='" . $_SESSION['user_login_id'] . "' ";
            mysql_query($sql_update) or die("update");
        } else {
            echo "eror";
        }
    }
    public function delete($id){
        if($_SERVER["REQUEST_METHOD"]=='POST'){
            $result = $this->_model->delete($id);
            if($result !== false){
                header('Location: blog');
                exit;
            }
        }
    }

}